package nus.mtech.bill;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.MessageFormat;
import java.util.Date;
import java.util.ResourceBundle;

public class SimpleTextBillFormatter implements BillFormatter {
	private static ResourceBundle bundle = ResourceBundle.getBundle("MessagesBundle");
	public Object format(Bill bill) {
		StringBuilder sb = new StringBuilder();
		sb.append(bundle.getString("company.name"));
		sb.append(bundle.getString("company.address"));
		String [] args = bill.getCustomerAddress() == null?new String[]{"N/A"}:(String[])bill.getCustomerAddress().split(";");
		sb.append(MessageFormat.format(bundle.getString("customer.address"),args));
		sb.append(MessageFormat.format(bundle.getString("general.info"), 
				bill.getAccountNo(), bill.getBillDate(), bill.getDueDate(),
				bill.getPreviousBalance(), bill.getPreviousBalance(), bill.getDueDate(), 
				bill.getTotalBill(), bill.getTotalBill()));
		sb.append(MessageFormat.format(bundle.getString("summary"), bill.getBillDate(), bill.getPreviousBalance(), bill.getPreviousBalance()));
		if(bill.getDigitalVoiceSubscriptionChargesSummary() != 0){
			sb.append(MessageFormat.format(bundle.getString("digital.voice"), bill.getDigitalVoiceSubscriptionChargesSummary(), bill.getDigitalVoiceUsageChargesSummary(), 
					bill.getDigitalVoiceSubscriptionChargesSummary() + bill.getDigitalVoiceUsageChargesSummary()));
		}
		if(bill.getMobileVoiceSubscriptionChargesSummary() != 0){
			sb.append(MessageFormat.format(bundle.getString("mobile.voice"), bill.getMobileVoiceSubscriptionChargesSummary(), bill.getMobileVoiceUsageCharges(), 
					bill.getMobileVoiceSubscriptionChargesSummary(), bill.getMobileVoiceUsageCharges()));
		}
		if(bill.getCableTVSubscriptionChargesSummary()!=0){
			sb.append(MessageFormat.format(bundle.getString("cable.tv"), bill.getCableTVSubscriptionChargesSummary(), bill.getAdditionalCableTVChannelsCharges(), 
					bill.getCableTVSubscriptionChargesSummary() + bill.getAdditionalCableTVChannelsCharges()));
		}
		
		sb.append(MessageFormat.format(bundle.getString("total"), bill.getTotalGST(), bill.getTotalBill()));
		sb.append(bundle.getString("account.detail"));
		
		if(bill.getDigitalVoiceSubscriptionChargesSummary() != 0){
			double line = bill.getDetailDigitalVoiceSubChargeLine();
			double transfer = bill.getDetailDigitalVoiceSubCallTransfer();
			double local = bill.getDetailDigitalVoiceUsageChargeLocalCalls();
			double idd = bill.getDetailDigitalVoiceUsageChargeIDDCalls();
			double total = line + transfer + local + idd;
			sb.append(MessageFormat.format(bundle.getString("digital.voice.detail"), 
					bill.getDigitalVoiceNumber(),
					line, 
					transfer,
					local,
					idd,
					total
					));
		}
		if(bill.getMobileVoiceSubscriptionChargesSummary() != 0){
			double roaming = bill.getDetailMobileVoiceChargeSubRoaming();
			double data = bill.getDetailMobileVoiceChargeSubDataServices();
			double local = bill.getDetailMobileVoiceUsageChargeLocalCalls();
			double idd = bill.getDetailMobileVoiceUsageChargeIDDCalls();
			double roamingCall = bill.getDetailMobileVoiceUsageChargeRoamingCalls();
			double total = roaming +data + local+idd+roamingCall;
			sb.append(MessageFormat.format(bundle.getString("mobile.voice.detail"), 					
					bill.getMobileVoiceNumber(),
					roaming, 
					data,
					local,
					idd,
					roamingCall,
					total
					));
		}
		if(bill.getCableTVSubscriptionChargesSummary()!=0){
			sb.append(MessageFormat.format(bundle.getString("cable.tv.detail"), 
					bill.getCableTVSubscriptionChargesSummary(),
					Double.parseDouble(bill.getCableTVChannelRate()),
					bill.getNoOfCableTVChannels(),
					bill.getAdditionalCableTVChannelsCharges(),
					bill.getCableTVSubscriptionChargesSummary() + bill.getAdditionalCableTVChannelsCharges()
					));
		}
		sb.append(MessageFormat.format(bundle.getString("total.detail"), bill.getTotalGST(), bill.getTotalBill()));
		return sb.toString();
	}
}
